home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The CICA Windows Explosion!
/
The CICA Windows Explosion! - Disc 2.iso
/
programr
/
eckelt01.zip
/
14
/
TSTACK.H
< prev
next >
Wrap
C/C++ Source or Header
|
1995-02-23
|
3KB
|
117 lines
// File from page 598 in "Thinking in C++" by Bruce Eckel
//////////////////////////////////////////////////
// From the compressed package ECKELT01.ZIP 2/21/95
// Copyright (c) Bruce Eckel, 1995
// Source code file from the book "Thinking in C++",
// Prentice Hall, 1995, ISBN: 0-13-917709-4
// All rights reserved EXCEPT as allowed by the following
// statements: You may freely use this file for your own
// work, including modifications and distribution in
// executable form only. You may copy and distribute this
// file, as long as it is only distributed in the complete
// (compressed) package with the other files from this
// book and you do not remove this copyright and notice.
// You may not distribute modified versions of the source
// code in this package. This package may be freely placed
// on bulletin boards, internet nodes, shareware disks and
// product vendor disks. You may not use this file in
// printed media without the express permission of the
// author. Bruce Eckel makes no
// representation about the suitability of this software
// for any purpose. It is provided "as is" without express
// or implied warranty of any kind. The entire risk as to
// the quality and performance of the software is with
// you. Should the software prove defective, you assume
// the cost of all necessary servicing, repair, or
// correction.
// If you think you've found an error, please
// email all modified files with loudly commented changes
// to: eckel@aol.com (please use the same
// address for non-code errors found in the book).
//////////////////////////////////////////////////
//: TSTACK.H -- Stack using templates
#ifndef TSTACK_H_
#define TSTACK_H_
// Some implementations require this:
template<class T> class tstackIterator;
template<class T>
class tstack {
struct link {
T* data;
link* next;
link(T* Data, link* Next) {
data = Data;
next = Next;
}
} * head;
int owns;
public:
tstack(int Owns = 1) : head(0), owns(Owns) {}
~tstack();
void push(T* Data) {
head = new link(Data,head);
}
T* peek() const { return head->data; }
T* pop();
int Owns() const { return owns; }
void Owns(int newownership) {
owns = newownership;
}
friend class tstackIterator<T>;
};
template<class T>
T* tstack<T>::pop() {
if(head == 0) return 0;
T* result = head->data;
link* oldHead = head;
head = head->next;
delete oldHead;
return result;
}
template<class T>
tstack<T>::~tstack() {
link* cursor = head;
while(head) {
cursor = cursor->next;
// Conditional cleanup of data:
if(owns) delete head->data;
delete head;
head = cursor;
}
}
template<class T>
class tstackIterator {
tstack<T>::link* p;
public:
tstackIterator(const tstack<T>& tl)
: p(tl.head) {}
tstackIterator(const tstackIterator& tl)
: p(tl.p) {}
// operator++ returns boolean indicating end:
int operator++() {
if(p->next)
p = p->next;
else p = 0; // Indicates end of list
return int(p);
}
int operator++(int) { return operator++(); }
// Smart pointer:
T* operator->() const {
if(!p) return 0;
return p->data;
}
T* current() const {
if(!p) return 0;
return p->data;
}
// int conversion for conditional test:
operator int() const { return p ? 1 : 0; }
};
#endif // TSTACK_H_